// pages/coupons/coupons.js
Page({
    data: {
      coupons: [],          // 优惠券列表
      categories: [         // 分类标签
        { id: 0, name: '全部', selected: true },
        { id: 1, name: '未使用', selected: false },
        { id: 2, name: '已使用', selected: false },
        { id: 3, name: '已过期', selected: false }
      ],
      searchText: '',       // 搜索关键词
      currentPage: 1,       // 当前页码
      isLoading: false      // 加载状态
    },
  
    onLoad() {
      this.generateMockData()
    },
  
    // 生成模拟数据
    generateMockData() {
      const newCoupons = Array.from({ length: 20 }, (_, i) => ({
        id: i + 1,
        name: `满${100 + i * 10}减${20 + i}`,
        type: Math.random() > 0.3 ? 'discount' : 'cash',
        value: 20 + i,
        status: ['unused', 'used', 'expired'][Math.floor(Math.random() * 3)],
        endTime: new Date(Date.now() + (i - 10) * 86400000).toLocaleDateString(),
        description: '限时优惠，仅限APP内使用'
      }))
      
      this.setData({
        coupons: newCoupons,
        filteredCoupons: newCoupons
      })
    },
  
    // 分类切换
    handleCategoryChange(e) {
      const id = e.currentTarget.dataset.id
      const categories = this.data.categories.map(cat => ({
        ...cat,
        selected: cat.id === id
      }))
      
      const status = id === 0 ? '' : 
        id === 1 ? 'unused' :
        id === 2 ? 'used' : 'expired'
  
      const filtered = this.data.coupons.filter(coupon => 
        (!status || coupon.status === status)
      )
  
      this.setData({
        categories,
        filteredCoupons: filtered,
        currentPage: 1
      })
    },
  
    // 搜索功能
    onSearchInput(e) {
      const searchText = e.detail.value.toLowerCase()
      const filtered = this.data.coupons.filter(coupon => 
        coupon.name.toLowerCase().includes(searchText)
      )
      this.setData({ searchText, filteredCoupons: filtered })
    },
  
    // 领取优惠券
    claimCoupon(e) {
      const index = e.currentTarget.dataset.index
      const coupons = this.data.filteredCoupons
      coupons[index].status = 'unused'
      this.setData({ filteredCoupons: coupons })
    }
  })